package com.ruoyi.ylyx.controller;

import java.util.List;

import com.ruoyi.ylyx.domain.SysDtArea;
import com.ruoyi.ylyx.service.ISysDtAreaService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ylyx.domain.YlyxUser;
import com.ruoyi.ylyx.service.IYlyxUserService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户信息Controller
 * 
 * @author yuexl
 * @date 2021-02-05
 */
@Controller
@RequestMapping("/ylyx/user")
public class YlyxUserController extends BaseController
{
    private String prefix = "ylyx/user";

    @Autowired
    private IYlyxUserService ylyxUserService;
    @Autowired
    private ISysDtAreaService sysDtAreaService;

    @RequiresPermissions("ylyx:user:view")
    @GetMapping()
    public String user()
    {
        return prefix + "/user";
    }

    /**
     * 查询用户信息列表
     */
    @RequiresPermissions("ylyx:user:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(YlyxUser ylyxUser)
    {
        startPage();
        List<YlyxUser> list = ylyxUserService.selectYlyxUserList(ylyxUser);
        return getDataTable(list);
    }

    /**
     * 导出用户信息列表
     */
    @RequiresPermissions("ylyx:user:export")
    @Log(title = "用户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(YlyxUser ylyxUser)
    {
        List<YlyxUser> list = ylyxUserService.selectYlyxUserList(ylyxUser);
        ExcelUtil<YlyxUser> util = new ExcelUtil<YlyxUser>(YlyxUser.class);
        return util.exportExcel(list, "user");
    }

    /**
     * 新增用户信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户信息
     */
    @RequiresPermissions("ylyx:user:add")
    @Log(title = "用户信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(YlyxUser ylyxUser)
    {
        return toAjax(ylyxUserService.insertYlyxUser(ylyxUser));
    }

    /**
     * 修改用户信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        YlyxUser ylyxUser = ylyxUserService.selectYlyxUserById(id);
        mmap.put("ylyxUser", ylyxUser);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户信息
     */
    @RequiresPermissions("ylyx:user:edit")
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(YlyxUser ylyxUser)
    {
        return toAjax(ylyxUserService.updateYlyxUser(ylyxUser));
    }

    /**
     * 删除用户信息
     */
    @RequiresPermissions("ylyx:user:remove")
    @Log(title = "用户信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(ylyxUserService.deleteYlyxUserByIds(ids));
    }

    /**
     * 启用
     */
    @RequiresPermissions("ylyx:user:list")
    @PostMapping("/openUser")
    @ResponseBody
    public AjaxResult openUser(String id)
    {
        YlyxUser ylyxUser = new YlyxUser();
        ylyxUser.setId(id);
        ylyxUser.setIsDisable("0");
        int num = ylyxUserService.updateYlyxUser(ylyxUser);
        return toAjax(num);
    }

    /**
     * 禁用
     */
    @RequiresPermissions("ylyx:user:list")
    @PostMapping("/closeUser")
    @ResponseBody
    public AjaxResult closeUser(String id)
    {
        YlyxUser ylyxUser = new YlyxUser();
        ylyxUser.setId(id);
        ylyxUser.setIsDisable("1");
        int num = ylyxUserService.updateYlyxUser(ylyxUser);
        return toAjax(num);
    }

    /**
     * 获取省
     */
    @GetMapping("/{parentId}")
    @ResponseBody
    public TableDataInfo provinceData(@PathVariable("parentId") String parentId)
    {
        SysDtArea sysDtArea = new SysDtArea();
        sysDtArea.setAreaParentId(parentId);
        List<SysDtArea> proList = sysDtAreaService.selectSysDtAreaList(sysDtArea);
        return getDataTable(proList);
    }
}
